package io.netty.example.test.my;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.example.test.SocketConstants;
import io.netty.handler.timeout.ReadTimeoutException;

import java.net.ConnectException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author John zhang
 * @version 0.1
 */
public class DelegateClientChannelInBoundHandler extends ChannelInboundHandlerAdapter {

	private static final Logger logger = Logger.getLogger ( DelegateClientChannelInBoundHandler.class.getSimpleName () );

	private boolean isSyncRead = false;
	private Object syncObject;

	@Override
	public void channelRead( ChannelHandlerContext ctx, Object msg ) throws Exception {
		super.channelRead ( ctx, msg );
		String result = (String) msg;
		if ( isSyncRead ) {
			syncObject = result;
			isSyncRead = false;
			synchronized (DelegateClient.class) {
				DelegateClient.class.notifyAll ();
			}
		}
		System.out.println ( result );
		String action = ctx.channel ().attr ( SocketConstants.actionKey ).get ();
		System.out.println ( "action : " + action );
		// ctx.write ( "\r\n" );
	}

	@Override
	public void channelReadComplete( ChannelHandlerContext ctx ) throws Exception {
		super.channelReadComplete ( ctx );
		logger.log ( Level.INFO, "channelReadComplete" );
	}

	@Override
	public void exceptionCaught( ChannelHandlerContext ctx, Throwable cause ) throws Exception {
		if ( cause instanceof ConnectException ) {
			logger.log ( Level.INFO, "网络访问发生异常" );
			ctx.close ();
		} else if ( cause instanceof ReadTimeoutException ) {
			logger.log ( Level.WARNING, "读超时异常", cause );
		}
	}

	public void setSyncReadOperation( Object syncObject ) {
		isSyncRead = true;
		this.syncObject = syncObject;
	}

	@Override
	public void channelRegistered( ChannelHandlerContext ctx ) throws Exception {
		// TODO Auto-generated method stub
		super.channelRegistered ( ctx );
		logger.log ( Level.INFO, "channelRegistered" );
	}

	@Override
	public void channelUnregistered( ChannelHandlerContext ctx ) throws Exception {
		// TODO Auto-generated method stub
		super.channelUnregistered ( ctx );
		logger.log ( Level.INFO, "channelUnregistered" );
	}

	@Override
	public void channelActive( ChannelHandlerContext ctx ) throws Exception {
		// TODO Auto-generated method stub
		super.channelActive ( ctx );
		logger.log ( Level.INFO, "channelActive" );
	}

	@Override
	public void channelInactive( ChannelHandlerContext ctx ) throws Exception {
		// TODO Auto-generated method stub
		super.channelInactive ( ctx );
		logger.log ( Level.INFO, "channelInactive" );
	}

	@Override
	public void userEventTriggered( ChannelHandlerContext ctx, Object evt ) throws Exception {
		// TODO Auto-generated method stub
		super.userEventTriggered ( ctx, evt );
		logger.log ( Level.INFO, "userEventTriggered" );
	}

	@Override
	public void channelWritabilityChanged( ChannelHandlerContext ctx ) throws Exception {
		super.channelWritabilityChanged ( ctx );
		logger.log ( Level.INFO, "channelWritabilityChanged" );
	}

}
